<template>
  <div>
    <!-- 虚拟车管理 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhVirtualCarMaintenance"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" :click="importHandle">导 入</multiuse-button>
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <BaseDialog :visible.sync="isImport" title="数据导入" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ImportFile :importData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, vehicleConfig, getWarehouseList, exportExcel ,exporturl} from '@/api/omSale/HeadquartersInventory/VirtualCarMaintenance';
import { exportFile } from '@/utils/index';
import ImportFile from './ImportFile';

export default {
  name: 'qjhVirtualCarMaintenance',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: { xhrAbort: true },
      formData: {},
      issearch: false,
      isImport: false,
      dealerCode: '',
      exportLoading: false
    };
  },
  components: { ImportFile },
  mounted() {
    this.getList();
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.vehicleInfo();
  },

  methods: {
    // 导入
    importHandle() {
      this.isImport = true;
      this.formData.name = '虚拟车管理导入';
      this.formData.upUrl = 'https://bjev-cms-oss-public.oss-cn-beijing.aliyuncs.com/虚拟车管理导入.xlsx';
      this.formData.importUrl = '/api/cmscloud.wholesales/virtualCar/upload';
    },
    emitCloseHandler(val, type) {
      this.isImport = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    // 仓库名称
    async getList() {
      const res = await getWarehouseList();
      if (res.resultCode == 200) {
        res.data.list.map(x => {
          this.topFilterList.find(item => item.fieldName === 'subWareHouseId').itemList.push({ text: x.warehouseName, value: x.warehouseId });
        });
      }
    },
    // 车型选择
    async vehicleInfo() {
      let res = await vehicleConfig(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    // 导出
    async exportHandle() {

      let json = Object.assign({}, { CODE: 'CODE_0000046' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportFile(response.data, '虚拟车管理', 'csv');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    seacherFun() {
      this.issearch = true;
    },
    ownercloseHandler(val) {
      if (val) {
        console.log(val);
        this.topFilterList.map(x => {
          if (x.fieldName == 'dealerName') {
            x.initialValue = val.dealerName;
          }
        });
        this.dealerCode = val.dealerCode;
      }
      this.issearch = false;
    },
    changeHandle(params) {
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    resetChange(val) {
      this.dealerCode = '';
      this.fetchParams.dealerCode = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'SELECT',
          label: '仓库名称',
          fieldName: 'subWareHouseId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '车辆状态',
          fieldName: 'vehicleStatus',
          placeholder: '请选择',
          itemList: this.createDictList('6040')
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '是否锁定',
          fieldName: 'isLock',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'RANGE_DATE',
          label: '总部入库日期',
          fieldName: 'warehousingStartDate|warehousingSEndDate',
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '订单占用',
          fieldName: 'matchFlag',
          placeholder: '请选择',
          itemList: [
            { text: '占用', value: 10041001 },
            { text: '可用', value: 10041002 }
          ]
        },
        {
          type: 'SELECT',
          label: '退车标记',
          fieldName: 'returnFlag',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'RANGE_DATE',
          label: '店端入库日期',
          fieldName: 'receiveStartDate|receiveEndDate',
          dateType: 'exactdaterange'
        },
        {
          type: 'RANGE_DATE',
          label: '实销日期',
          fieldName: 'reportCheckStartDate|reportCheckEndDate',
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '补贴状态',
          fieldName: 'subsidyStatus',
          placeholder: '请选择',
          itemList: [
            { text: '已上报', value: 10041001 },
            { text: '未上报', value: 10041002 }
          ]
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'SELECT',
          label: '是否虚拟车',
          fieldName: 'isVirtual',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: props => {
            return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'outColorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'subWareHouseName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车辆状态',
          dataIndex: 'vehicleStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [{ text: '车厂库存', value: 60401001 }],
          render: props => {
            return <span>{this.createDictText(props.row.vehicleStatus, '6040')}</span>;
          }
        },
        {
          title: '是否虚拟车',
          dataIndex: 'isVirtual',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isVirtual, '1004')}</span>;
          }
        },
        {
          title: '订单占用',
          dataIndex: 'matchFlag',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '占用', value: 10041001 },
            { text: '可用', value: 10041002 }
          ],
          render: props => {
            return <span>{props.row.matchFlag == 10041001 ? '占用' : '可用'}</span>;
          }
        },
        {
          title: '是否锁定',
          dataIndex: 'isLock',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isLock, '1004')}</span>;
          }
        },
        {
          title: '锁定原因',
          dataIndex: 'lockRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: props => {
            return <span>{this.createDictText(props.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '订单状态',
          dataIndex: 'doOrderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6002'),
          render: props => {
            return <span>{this.createDictText(props.row.doOrderStatus, '6002')}</span>;
          }
        },
        {
          title: '零售订单号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '是否三方车辆',
          dataIndex: 'isTripartite',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isTripartite, '1004')}</span>;
          }
        },
        {
          title: '三方票号',
          dataIndex: 'biilNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退车标识',
          dataIndex: 'returnFlag',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.returnFlag, '1004')}</span>;
          }
        },
        {
          title: '生产下线日期',
          dataIndex: 'productDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '销司入库日期',
          dataIndex: 'vehicleCreatedAt',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '车龄(天)',
          dataIndex: 'productDateAge',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库龄(天)',
          dataIndex: 'postingDateAge',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发动机号',
          dataIndex: 'engineNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公告号',
          dataIndex: 'announceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '合格证号',
          dataIndex: 'certificateNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电机号',
          dataIndex: 'motorNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电池号',
          dataIndex: 'batteryNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销司配车日期',
          dataIndex: 'matchDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '销司出库日期',
          dataIndex: 'sapOutDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '发运日期',
          dataIndex: 'startDeliveryDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '店端入库日期',
          dataIndex: 'receiveDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },

        {
          title: '店端配车日期',
          dataIndex: 'distributionDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },

        {
          title: '店端交车日期',
          dataIndex: 'stockOutDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },

        {
          title: '实销日期',
          dataIndex: 'reportCheckDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },

        {
          title: '上牌日期',
          dataIndex: 'plateCheckDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '补贴资料提报日期',
          dataIndex: 'subApplyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        }
      ];
    }
  }
};
</script>
